Happy Git and Github for the useR
Session 04 - Git fundamentals

Boook club R-Ladies Bergen, R-Ladies Den Bosch, R-Ladies Amsterdam

Book by Jenny, presentation by Michelle

Program for today

  • Some Git basics
    • 20 Repo, commit, diff, tag
    • 21 Git commands
    • 22 Branches
    • 23 Remotes
    • 24 Refs

How Git works, concepts, applying it to data science

Chapter 20: Repo, commit, diff, tag

Terms

  • Repo (repository): set of files
  • Commit: snapshot of current version of files in a project/repo
  • Diff: differences between one commit and another commit. Each Git version of a file is an accumulation of diffs
  • SHA (Secure Hash Algorithm): a string of 40 letters and numbers assigned by Git to a commit to uniquely identify it
  • Tag: a name you can assign to a version, e.g. “v.1.0.3” or “draft-01”

Advice

For each project:

  • Assign it to one local directory
  • Make it an RStudio project
  • Make it a Git repository (see previous book section)

Workflow

  • Work on your files locally
  • Periodically make a commit
    • When a “significant” stage is reached
    • Include a short commit message motivating the change
  • Periodically push commits to GitHub - makes current version of repo accessible to others
    • First pull so that you have the updated remote version

Workflow

Chapter 21: Git commands

Can you remember/guess what these commands do?

  • git clone https://github.com/jennybc/happy-git-with-r.git
  • git remote --verbose
  • git add foo.txt : add foo.txt to the index (staging area)
  • git commit --message "A commit message"
  • git status

Can you remember/guess what these commands do?

  • git log
  • git log --oneline

Can you remember/guess what these commands do?

  • git diff

  • The rest of the list is covered in the next few chapters

Chapter 22: Branches

Branching and merging

  • For parallel work or experimenting with new features without interfering with the main project
  • git branch issue-5
  • git checkout issue-5
  • git checkout -b issue-5
  • Switching branch when you have incomplete work:
    • git commit --all -m "WIP"
    • git checkout main
    • git checkout issue-5
    • git reset HEAD^

Merging and handling conflicts

  • git checkout main

  • git merge issue-5

Merging and handling conflicts

  • git add index.html
  • git commit

Chapter 23: Remotes

Remotes

  • Remote repositories are hosted on a network (not your local version)
  • git clone
  • git remote add happygit https://github.com/jennybc/happy-git-with-r.git
  • Adding a second remote is useful when you have forked and cloned a repo and want to pull changes from the original repository (not your forked remote) - this second remote is usually nicknamed upstream:
  • git remote add upstream https://github.com/TRUE_OWNER/REPO.git

Fetching and pushing

  • git fetch happygit: downloads the remote commits to your local repo without changing the local branch
  • git fetch + git mergegit pull
  • Git pull vs git fetch: https://www.youtube.com/watch?v=T13gDBXarj0
  • # push my local changes to the origin remote's main branch git push origin main

Upstream tracking branches

  • git config --global push.default current

Chapter 24: Refs

What are refs?

  • References to specific commits (like pointers in programming). Examples:
    • a branch name
    • HEAD (a symbolic ref)
    • a tag (e.g., v1.4.2)

  • Use refs in commands like git diff, git reset and git checkout

Relative refs

Copying a specific SHA is easy in visual Git tools like GitHub and GitKraken

Pro Git

The end of the session 4!